package com.bite.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.bite.order.handler.ExceptionHandlerUtil;
import com.bite.order.model.OrderInfo;
import com.bite.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

//    @SentinelResource(value = "/sentinel/id", blockHandler = "handleException")
    @SentinelResource(value = "/sentinel/id", blockHandlerClass = ExceptionHandlerUtil.class, blockHandler = "handleException")
    @RequestMapping("/{orderId}")
    public OrderInfo getOrderById(@PathVariable("orderId") Integer orderId) {
//        try {
//            OrderInfo orderInfo = orderService.selectOrderById(orderId);
//            return orderInfo;
//        } catch (Exception e) {
//            log.error("查询订单失败, orderId:{}", orderId, e);
//            return new OrderInfo();
//        }
        return orderService.selectOrderById(orderId);
    }

    public OrderInfo handleException(Integer orderId, BlockException e) {
        System.out.println("进⼊异常处理⽅法");
        if (e instanceof FlowException) {
            return new OrderInfo();
        } else if (e instanceof AuthorityException) {
            return new OrderInfo();
        } else if (e instanceof DegradeException) {
            return new OrderInfo();
        } else if (e instanceof ParamFlowException) {
            return new OrderInfo();
        }
        return new OrderInfo();
    }
}
